import 'package:flutter/material.dart';
import 'package:hockey/jzx_apps.dart';
import 'package:hockey/jzx_models.dart';
import 'package:hockey/services/jzx_user.dart';

import '../jzx_posts/view.dart';
import 'controller.dart';

class JzxUserHomepageView extends GetView<JzxUserHomepageController> {
  const JzxUserHomepageView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      resizeToAvoidBottomInset: false,
      appBar: JzxAAppBar(
        actions: [
          if (JzxUserService.to.uid != controller.uid)
            InkWell(
                onTap: () {
                  controller.showOptions();
                },
                child: Assets.images.icNavMore
                    .image(width: 24)
                    .paddingOnly(right: 20)),
        ],
      ),
      backgroundColor: Colors.black,
      floatingActionButton: JzxUserService.to.uid == controller.uid
          ? const SizedBox.shrink()
          : Obx(
              () => GradientButton(
                controller.isFollow.value ? 'Following' : 'Follow +',
                onPressed: () {
                  controller.followAct();
                },
                width: 194.w,
                height: 46.w,
              ),
            ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      body: Stack(
        children: [
          Assets.images.normalBgv.image(),
          Positioned.fill(
            top: kNavigationBarHeight,
            child: Stack(
              alignment: Alignment.topCenter,
              children: [
                Positioned.fill(
                  top: 72.w,
                  child: Container(
                    padding: EdgeInsets.only(top: 72.w),
                    width: double.infinity,
                    decoration: const BoxDecoration(
                        gradient: LinearGradient(
                          colors: [
                            Color(0xFF487356),
                            Colors.black87,
                            Colors.black,
                          ],
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          stops: [0.05, 0.31, 1.0],
                        ),
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(24),
                          topRight: Radius.circular(24),
                        )),
                    child: Column(
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            ConstrainedBox(
                              constraints: BoxConstraints(
                                maxWidth: Get.width - 110.w - 55.w,
                              ),
                              child: Text(
                                controller.user.value.nickname,
                                style: myAppColors.white.font_20.medium,
                                maxLines: 1,
                              ),
                            ),
                            Image.asset(
                                'assets/images/ic_level${controller.user.value.level.levelFormat}.png',
                                width: 55.w),
                          ],
                        ).paddingOnly(left: 55.w, bottom: 5),
                        Container(
                          constraints: BoxConstraints(maxWidth: 180.w),
                          padding: const EdgeInsets.symmetric(
                              horizontal: 12, vertical: 8),
                          decoration: BoxDecoration(
                            color: const Color(0x40000000),
                            borderRadius: BorderRadius.circular(100),
                          ),
                          child: Text(
                            'ID：${controller.user.value.userId}',
                            style: myAppColors.text4.font_12.medium,
                            maxLines: 1,
                            // overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Expanded(
                          child: Obx(() {
                            if (controller.items.isEmpty) {
                              return Center(
                                child: Assets.images.icEmpty
                                    .image(width: 200.w)
                                    .marginOnly(bottom: 80.w),
                              );
                            }
                            return GridView.builder(
                              padding: const EdgeInsets.all(20),
                              gridDelegate:
                                  const SliverGridDelegateWithFixedCrossAxisCount(
                                      crossAxisCount: 2,
                                      mainAxisSpacing: 11,
                                      crossAxisSpacing: 11,
                                      childAspectRatio: 162 / 200),
                              itemCount: controller.items.length,
                              itemBuilder: (context, index) {
                                final item = controller.items[index];
                                return JzxPostItemView(
                                  item: item,
                                  index: index,
                                  height: index == 1 ? 184.w : 200.w,
                                  canPushUser: false,
                                  onLike: () {
                                    controller.likeOrCancel(item, index);
                                  },
                                );
                              },
                            );
                          }),
                        )
                      ],
                    ),
                  ),
                ),
                JzxUserFrame(
                  controller.user.value.avatar,
                  144.w,
                  level: controller.user.value.level.levelFormat,
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
